package com.zhiyu.service.impl;

import com.zhiyu.common.RoleType;
import com.zhiyu.entity.DO.RoleDO;
import com.zhiyu.entity.DO.UserDO;
import com.zhiyu.mapper.RoleMapper;
import com.zhiyu.mapper.UserMapper;
import com.zhiyu.mapper.UserRoleMapper;
import com.zhiyu.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    public void addRole(int userId, RoleType roleType) {
        System.err.println(userId);
        //查看用户是否存在
        UserDO userDO = userMapper.findById(userId);
        if(userDO == null)
            return;
        System.err.println(1);

        //查看是否有这个角色
        String roleName = roleType.getName();
        RoleDO roleDO = roleMapper.findByName(roleName);
        if(roleDO == null)
            return;

        userRoleMapper.insert(userId, roleDO.getId());
    }
}
